#include "common/logger/logger.h"
#include <array>
#include <common/ipconverter/ipconverter.h>
#include <sstream>

int64_t IpConverter::ipAddress2uint64t(const std::string& ipStr)
{
	std::array<int64_t, 4> parts{};
	std::istringstream iss(ipStr);
	std::string token;
	int i = 0;

	while (std::getline(iss, token, '.'))
	{
		if (i >= 4)
		{
			throw std::invalid_argument("Invalid IPv4: too many parts");
		}
		int num = std::stoi(token);
		if (num < 0 || num > 255)
		{
			throw std::invalid_argument("IPv4 part out of range");
		}
		parts[i++] = static_cast<int64_t>(num);
	}
	if (i != 4)
	{
		throw std::invalid_argument("Invalid IPv4: too few parts");
	}

	return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3];
}
std::string IpConverter::int64t2IpAddress(int64_t ipNum)
{
	std::ostringstream oss;
	oss << ((ipNum >> 24) & 0xFF) << "."
		<< ((ipNum >> 16) & 0xFF) << "."
		<< ((ipNum >> 8) & 0xFF) << "."
		<< (ipNum & 0xFF);
	return oss.str();
}